package com.springboot.service.job;

import org.quartz.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;

/**
 * @author Administrator
 */
@Component
public class QuartzManager {

    private static final Logger logger = LoggerFactory.getLogger(QuartzManager.class);

    @Autowired
    private Scheduler scheduler;

    public void startJob(String cron, String jobName,
                         String groupName, Object obj, Method method) throws SchedulerException {
        startJob(scheduler, cron, jobName, groupName, obj, method);
    }

    public void stopJob() {
        removeJob("job1", "group1");
    }

    private void startJobDemo(Scheduler scheduler) throws SchedulerException {
        // 通过JobBuilder构建JobDetail实例，JobDetail规定只能是实现Job接口的实例
        // JobDetail 是具体Job实例
        //传参数到TestQuartzJob中,供其使用
        JobDetail jobDetail = JobBuilder.newJob(TestQuartzJob.class).withIdentity("job1", "group1").build();
        jobDetail.getJobDataMap().put("jobName", "job1");
        // 基于表达式构建触发器
        CronScheduleBuilder cronScheduleBuilder = CronScheduleBuilder.cronSchedule("0/5 * * * * ?");
        // CronTrigger表达式触发器 继承于Trigger
        // TriggerBuilder 用于构建触发器实例
        CronTrigger cronTrigger = TriggerBuilder.newTrigger().withIdentity("job1", "group1")
                .withSchedule(cronScheduleBuilder).build();
        //开启定时任务
        scheduler.scheduleJob(jobDetail, cronTrigger);
        //启动定时器
        scheduler.start();
    }

    private void startJob(Scheduler scheduler, String cron, String jobName,
                          String groupName, Object obj, Method method) throws SchedulerException {

        // 通过JobBuilder构建JobDetail实例，JobDetail规定只能是实现Job接口的实例
        // JobDetail 是具体Job实例
        //传参数到TestQuartzJob中,供其使用
        JobDetail jobDetail = JobBuilder.newJob(TestQuartzJob.class).withIdentity(jobName, groupName).build();
        jobDetail.getJobDataMap().put("obj", obj);
        jobDetail.getJobDataMap().put("method", method);
        // 基于表达式构建触发器
        CronScheduleBuilder cronScheduleBuilder = CronScheduleBuilder.cronSchedule(cron);
        // CronTrigger表达式触发器 继承于Trigger
        // TriggerBuilder 用于构建触发器实例
        CronTrigger cronTrigger = TriggerBuilder.newTrigger().withIdentity(jobName, groupName)
                .withSchedule(cronScheduleBuilder).build();
        //开启定时任务
        scheduler.scheduleJob(jobDetail, cronTrigger);
        //启动定时器
        scheduler.start();
    }

    /**
     * 删除定时任务
     *
     * @param jobName
     * @param groupName
     */
    public void removeJob(String jobName, String groupName) {
        TriggerKey triggerKey = TriggerKey.triggerKey(jobName, groupName);
        try {
            if (checkExists(jobName, groupName)) {
                logger.info("开始关闭任务job1");
                //暂停触发器
                scheduler.pauseTrigger(triggerKey);
                //关闭触发器
                scheduler.unscheduleJob(triggerKey);
                logger.info("关闭任务job1成功");
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 判断任务是否存在
     *
     * @param jobName
     * @param groupName
     * @return
     * @throws SchedulerException
     */
    public boolean checkExists(String jobName, String groupName) throws SchedulerException {
        TriggerKey triggerKey = TriggerKey.triggerKey(jobName, groupName);
        return scheduler.checkExists(triggerKey);
    }

}
